#define _CRT_SECURE_NO_WARNINGS 1

class Solution
{
public:
    int lastStoneWeight(vector<int>& stones)
    {
        priority_queue<int> heap;
        for (auto& x : stones) heap.push(x);

        while (heap.size() >= 2)
        {
            int max = heap.top();
            heap.pop();
            int cmax = heap.top();
            heap.pop();
            if (cmax != max) heap.push(max - cmax);
        }
        if (!heap.empty())     return heap.top();
        else return 0;
    }
};